package com.fenghongzhang.day011;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.provider.Telephony;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        //6.0-5.0
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},999);
        }else{
            fangfaming();
        }

    }

    private void fangfaming() {
        //读取短信
        ContentResolver contentResolver = getContentResolver();

        Cursor query = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

        if (query != null) {
            while (query.moveToNext()) {
                String body = query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA));
                Toast.makeText(this, body, Toast.LENGTH_SHORT).show();
            }
            query.close();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 999 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            //读取短信
            fangfaming();
        }else{

        }
    }
}
